package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.apis.wemedia.IWemediaClient;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.HotArticleService;
import com.heima.common.redis.CacheService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.vos.HotArticleVo;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.pojos.WmChannel;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.heima.common.constants.ArticleConstants.*;

/**
 * @author JianKang
 * @create 2023-11-02 16:12
 */
@Service
@Transactional
@Slf4j
public class HotArticleServiceImpl implements HotArticleService {

    @Autowired
    ApArticleMapper apArticleMapper;

    @Autowired
    IWemediaClient wemediaClient;

    @Autowired
    CacheService cacheService;

    @Override
    public void computerHotArticle() {
        //1.查询前5天的数据
        Date date = DateTime.now().minusDays(50).toDate();
        List<ApArticle> apArticleList = apArticleMapper.findArticleListByLast5days(date);
        //2.计算文章的分值
        List<HotArticleVo> hotArticleVos = getHotArticleVos(apArticleList);
        //3.为每个频道缓存30条分值较高的文章
        cacheTagToRedis(hotArticleVos);
    }

    private void cacheTagToRedis(List<HotArticleVo> hotArticleVos) {
        //每个频道缓存30条分值较高的文章
        ResponseResult responseResult = wemediaClient.getChannels();
        if (responseResult.getCode().equals(200)) {
            List<WmChannel> wmChannels = JSON.parseArray(JSON.toJSONString(responseResult.getData()), WmChannel.class);
            if (wmChannels != null && !wmChannels.isEmpty()) {
                for (WmChannel wmChannel : wmChannels) {
                    List<HotArticleVo> hots = hotArticleVos.stream().filter(x -> x.getChannelId().equals(wmChannel.getId())).collect(Collectors.toList());
                    hots = hots.stream().sorted(Comparator.comparing(HotArticleVo::getScore).reversed()).collect(Collectors.toList());
                    if (hots.size() > 30) {
                        hots = hots.stream().limit(30).collect(Collectors.toList());
                    }
                    cacheService.set(HOT_ARTICLE_FIRST_PAGE + wmChannel.getId(), JSON.toJSONString(hots));
                }
            }
        }
    }

    private List<HotArticleVo> getHotArticleVos(List<ApArticle> apArticleList) {
        List<HotArticleVo> hotArticleVos = new ArrayList<>();
        if (apArticleList != null && !apArticleList.isEmpty()) {
            for (ApArticle apArticle : apArticleList) {
                HotArticleVo hotArticleVo = new HotArticleVo();
                BeanUtils.copyProperties(apArticle, hotArticleVo);
                hotArticleVo.setScore(computeScore(apArticle));
                hotArticleVos.add(hotArticleVo);
            }
        }
        return hotArticleVos;
    }

    private Integer computeScore(ApArticle apArticle) {
        Integer score = 0;
        if (apArticle.getLikes() != null) {
            score += apArticle.getLikes() * HOT_ARTICLE_LIKE_WEIGHT;
        }
        if (apArticle.getViews() != null) {
            score += apArticle.getViews() * HOT_ARTICLE_COMMENT_WEIGHT;
        }
        if (apArticle.getCollection() != null) {
            score += apArticle.getCollection() * HOT_ARTICLE_COLLECTION_WEIGHT;
        }
        return score;
    }
}
